﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class ChangeSignaturePassword : BasePage
    {
        private Sys_UserBLL sub = new Sys_UserBLL();
        private Sys_SignatureBLL ssb = new Sys_SignatureBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtUserCode.Text = lognUser.USER_LOGINNAME;
                txtUserName.Text = lognUser.USER_NAME;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                MessageBox.Show(Page, "用户名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtUserCode.Text.Trim()))
            {
                MessageBox.Show(Page, "登录名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtOldPassword.Text.Trim()))
            {
                MessageBox.Show(Page, "原密码不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtNewPassword.Text.Trim()))
            {
                MessageBox.Show(Page, "新密码不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtConfirmPassword.Text.Trim()))
            {
                MessageBox.Show(Page, "确认密码不能为空");
                return;
            }
            if (txtNewPassword.Text != txtConfirmPassword.Text)
            {
                MessageBox.Show(Page, "确认密码输入错误");
                return;
            }
            DataTable dt = ssb.GetSignture(lognUser.USER_ID, decimal.Parse(radlType.SelectedValue));
            //DataTable dt = ssb.GetInfoByCon("LogonName", txtUserCode.Text);
            Sys_Signature model = new Sys_Signature();

            if (dt != null && dt.Rows.Count > 0)
            {
                model.SIGNATUREID = decimal.Parse(dt.Rows[0]["SIGNATUREID"].ToString());
                model.SYSUSERID = decimal.Parse(dt.Rows[0]["SYSUSERID"].ToString());
                model.SIGNATURETYPE = decimal.Parse(dt.Rows[0]["SIGNATURETYPE"].ToString() == "私章" ? "1" : "2");
                model.SIGNATUREPWD = dt.Rows[0]["SIGNATUREPWD"].ToString();
                model.SIGNATUREPATH = dt.Rows[0]["SIGNATUREPATH"].ToString();
                model.STATE = decimal.Parse(dt.Rows[0]["STATE"].ToString());
                model.SIGNATURENAME = dt.Rows[0]["SIGNATURENAME"].ToString();
            }
            else
            {
                MessageBox.Show(Page, "不存在该用户");
                return;
            }
            string pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtOldPassword.Text.Trim(), "MD5");
            if (pwd != model.SIGNATUREPWD)
            {
                MessageBox.Show(Page, "原密码错误,请重新输入密码");
                return;
            }

            model.SIGNATUREPWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtNewPassword.Text.Trim(), "MD5");
            if (model.SIGNATURETYPE != decimal.Parse(radlType.SelectedValue))
            {
                string type = radlType.SelectedValue == "1" ? "私章" : "公章";
                MessageBox.Show(Page, "该用户不存在" + type + "");
                return;
            }
            int count = ssb.UPDATE(model, lognUser.USER_NAME);
            if (count > 0)
            {
                MessageBox.Show(Page, "保存成功");
                return;
            }
            else
            {
                MessageBox.Show(Page, "保存失败");
                return;
            }
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Right.aspx");
        }
    }
}